﻿using Kevin.Shop.Business.Comments.Components;
using Kevin.Shop.Business.Core.Components;
using Kevin.Shop.Business.Products.Components;
using Kevin.Shop.Business.Products.Components.Category;
using Kevin.Shop.Business.Service.Entity.Helper;
using RiderShop.Mobile.Common.Component;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace RiderShop.Mobile.Modle.Home
{
    public class SiteMapInfoViewModel
    {
        //private const string CrumbRowWithLinkTemplete = "<span><a href='{0}'>{1}</a></span>";
        //private const string CrumbRowWithOutLinkTemplete = "<span>{0}</span>";
        //private const string CrumbArrowTemplete = "<span>&nbsp;&gt;&nbsp;</span>";

        private const string CrumbRowWithLinkTemplete = "<a href='{0}'>{1}</a>";
        private const string CrumbRowWithOutLinkTemplete = "{0}";
        private const string CrumbArrowTemplete = "&nbsp;&gt;&nbsp;";
        private const string DynamicPageMark = "DynamicPage";

        private const string WebSite_Sort_CategoryWithBrand = "/Product/ProductList?gid={0}&brand={1}";
        private const string WebSite_Sort_Group = "/Product/ProductList?gid={0}";
        private const string WebSite_Sort_Subcateory = "/Product/ProductList?gid={0}&sid={1}";

        private const string WebSite_SystemTools_ArticleList = "/Content/AfficheList?type={0}&acid={1}";

        public string SiteInfo { get; set; }

        public SiteMapInfoViewModel()
        {
            if (SiteMap.CurrentNode != null)
            {
                SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
                if (currentNode.Description.ToLower() == DynamicPageMark.ToLower())
                {
                    //currentNode.Title = string.Empty;
                    //currentNode.Description = string.Empty;
                    //currentNode.Url = string.Empty;
                    //sitMapUC.Visible = false;
                    if (KevinContext.Current.GetIntFromQueryString("acId", Pagination.DefaultValue) > 0 ||
                        KevinContext.Current.GetIntFromQueryString("afId", Pagination.DefaultValue) > 0)
                    {
                        SiteInfo = BuildCrumbWithContent();
                    }
                    else
                    {
                        SiteInfo = BuildCrumbWithProduct();
                    }
                }
                else
                {
                    //sitMapUC.Visible = true;
                    //txtSiteInfo.Visible = false;

                    var crumbStrTemplete = new StringBuilder();

                    //crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, "/", "首页");
                    //crumbStrTemplete.Append(CrumbArrowTemplete);
                    crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, currentNode.Title);

                    SiteMapNode parentNode = currentNode.ParentNode;
                    while (parentNode != null)
                    {
                        crumbStrTemplete.Insert(0, CrumbArrowTemplete);
                        crumbStrTemplete.Insert(0, string.Format(CrumbRowWithLinkTemplete, parentNode.Url, parentNode.Title));
                        parentNode = parentNode.ParentNode;
                    }

                    SiteInfo = crumbStrTemplete.ToString();
                }
            }
            else
            {
                var crumbStrTemplete = new StringBuilder();
                crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, "/", "首页");
                SiteInfo = crumbStrTemplete.ToString();
            }

        }

        /**
                 面包屑URL规则：
                #1.通过groupId生成:	首页->group
                #2.通过categoryId生成： 首页->group->category
                #3.通过subcategory生成: 首页->group->category->subcategory
                #4.通过product生成：首页->group->category->subcategory->brand->product
                #5.只有subcategory，product page自定义生成，不使用sitemap，其他都使用
            <strong>
                  <span><a href="#" title="网上购物,电子商务,网上商城,网店系统，中小企业网店系统">首页</a></span>
                  <span> &gt;</span><span>电脑产品</span></span>
                  <span> &gt;</span><a href="#">笔记本</a></span>
                  <span> &gt;</span><a href="#">HP</a></span>
             </strong>
        ***/
        public string BuildCrumbWithProduct()
        {
            var crumbStrTemplete = new StringBuilder();
            int groupId = KevinContext.Current.GetIntFromQueryString("gid", Pagination.DefaultValue);
            int subcategoryId = KevinContext.Current.GetIntFromQueryString("sid", Pagination.DefaultValue);
            int brandId = KevinContext.Current.GetIntFromQueryString("brand", Pagination.DefaultValue);
            string productId = KevinContext.Current.QueryString["prd"];
            if (subcategoryId < 0)
            {
                subcategoryId = KevinContext.Current.GetIntFromQueryString("cid", Pagination.DefaultValue);
            }
            ProductBasicInfo product = null;

            crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, "/", "首页");

            if (!string.IsNullOrWhiteSpace(productId))
            {
                var products = RestfulHelper.Query<ProductBasicInfo>(SiteUrl.FormatUrl("ProductManagement_GetProductInfoByName", productId));
                if (products.Count > 0)
                {
                    product = products[0];
                    subcategoryId = product.CategoryId;
                }
            }
            if (brandId > 0)
            {
                var brands = RestfulHelper.Query<Manufactory>(SiteUrl.FormatUrl("ProductManagement_GetManufactoryList", brandId));
                if (brands.Count > 0)
                {
                    var brand = brands[0];
                    crumbStrTemplete.Append(CrumbArrowTemplete);
                    crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, string.Format(WebSite_Sort_CategoryWithBrand, groupId, brand.ManufactoryId), brand.ManufactoryName);
                }
            }
            else if (subcategoryId > 0)
            {
                var categoryInfos = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategoriesWithId", subcategoryId));
                if (categoryInfos.Count > 0)
                {
                    var currentCategory = categoryInfos[0];
                    groupId = currentCategory.GroupId;
                    if (currentCategory.GroupId > 0
                        && (currentCategory.Depth == CategoryLevelType.SubCategory || currentCategory.Depth == CategoryLevelType.Category))
                    {
                        string url = string.Format(WebSite_Sort_Group, currentCategory.GroupId);
                        crumbStrTemplete.Append(CrumbArrowTemplete);
                        crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, url, currentCategory.GroupName);
                    }
                    if (currentCategory.GroupId > 0 && (currentCategory.Depth == CategoryLevelType.Group))
                    {
                        crumbStrTemplete.Append(CrumbArrowTemplete);
                        crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, currentCategory.GroupName);
                    }
                    if (currentCategory.ParentCategoryId > 0 && (currentCategory.Depth == CategoryLevelType.SubCategory))
                    {
                        string url = string.Format(WebSite_Sort_Subcateory, groupId, currentCategory.ParentCategoryId);
                        crumbStrTemplete.Append(CrumbArrowTemplete);
                        crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, url, currentCategory.ParentCategoryName);
                    }

                    if (string.IsNullOrWhiteSpace(productId)
                        && (currentCategory.Depth == CategoryLevelType.SubCategory || currentCategory.Depth == CategoryLevelType.Category))
                    {
                        crumbStrTemplete.Append(CrumbArrowTemplete);
                        crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, currentCategory.CategoryName);
                    }
                    else
                    {
                        crumbStrTemplete.Append(CrumbArrowTemplete);
                        crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete,
                                                      string.Format(WebSite_Sort_Subcateory, groupId, currentCategory.CategoryId),
                                                      currentCategory.CategoryName);
                    }
                }
            }
            else if (groupId > 0)
            {
                var groups = RestfulHelper.Query<CategoryInfo>(SiteUrl.FormatUrl("ProductManagement_GetCategoriesWithId", groupId));
                if (groups.Count > 0)
                {
                    var group = groups[0];
                    string url = string.Format(WebSite_Sort_Group, group.CategoryId);
                    crumbStrTemplete.Append(CrumbArrowTemplete);
                    crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, url, group.CategoryName);
                }
            }

            if (!string.IsNullOrWhiteSpace(productId) && product != null)
            {
                //因为RongJi只有一个Group和品牌相同，有些重复
                //crumbStrTemplete.Append(CrumbArrowTemplete);
                //crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, string.Format(WebSite_Sort_CategoryWithBrand,
                //                              groupId, product.ManufactoryId), product.ManufactoryName);

                crumbStrTemplete.Append(CrumbArrowTemplete);
                crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, product.Title);
            }
            return crumbStrTemplete.ToString();
        }


        public string BuildCrumbWithContent()
        {
            var crumbStrTemplete = new StringBuilder();
            int? contentType = Pagination.DefaultValue;
            int contentCategoryId = KevinContext.Current.GetIntFromQueryString("acid", Pagination.DefaultValue);
            string articleId = KevinContext.Current.QueryString["afid"];

            ArticleInfo article = null;
            crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, "/", "首页");

            if (!string.IsNullOrWhiteSpace(articleId))
            {
                var articles = RestfulHelper.Query<ArticleInfo>(SiteUrl.FormatUrl("Service_SystemTools_Affiche", articleId));
                if (articles != null)
                {
                    if (articles.Count > 0)
                    {
                        article = articles[0];
                        contentCategoryId = article.ContentCategoryId;
                    }
                }
            }

            if (contentCategoryId > 0)
            {
                ArticleCategoryInfo articleCategoryInfo = null;
                var articleCategoryInfos = RestfulHelper.Query<ArticleCategoryInfo>(SiteUrl.FormatUrl("Service_SystemTools_ContentCategory", contentCategoryId));
                if (articleCategoryInfos != null)
                {
                    if (articleCategoryInfos.Count > 0)
                    {
                        articleCategoryInfo = articleCategoryInfos[0];
                        contentType = articleCategoryInfo.ContentType;
                        if (contentType == 0)
                        {
                            crumbStrTemplete.Append(CrumbArrowTemplete);
                            crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, "帮助中心");
                        }
                        if (contentType == 1)
                        {
                            crumbStrTemplete.Append(CrumbArrowTemplete);
                            crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, "文章列表");
                        }
                        crumbStrTemplete.Append(CrumbArrowTemplete);
                        if (article != null)
                        {
                            crumbStrTemplete.AppendFormat(CrumbRowWithLinkTemplete, string.Format(WebSite_SystemTools_ArticleList, contentType, contentCategoryId), articleCategoryInfo.CategoryName);
                        }
                        else
                        {
                            crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, articleCategoryInfo.CategoryName);
                        }
                        if (article != null)
                        {
                            crumbStrTemplete.Append(CrumbArrowTemplete);
                            crumbStrTemplete.AppendFormat(CrumbRowWithOutLinkTemplete, article.Title);
                        }
                    }
                }
            }
            return crumbStrTemplete.ToString();
        }


    }
}
